def pr(num):
    for i in range(2, num):
        if num%i == 0:
            return False
    return True

def adferc(num):
    if num <= 999 or num > 9999:
        return 0

    v1 = num // 1000
    v2 = (num%1000) // 100
    v3 = (num%100) // 10
    v4 = num%10
    max_ch = 0
    for ch in (num, v1, v2, v3, v4, v1*10 +v2,  v2*10 +v3, v3*10 +v4, v1*100 +v2*10+v3, v2*100 +v3*10+v4) :
        if pr(ch) and ch>max_ch:
            max_ch = ch
    return max_ch

num = int(input(''))
result = adferc(num)
print(result)
